<template>
    <div class="login-container">
      <el-card class="login-box">
        <h1 class="login-title">后台管理系统</h1>
  
        <el-form 
          ref="loginForm" 
          :model="loginForm" 
          :rules="loginRules" 
          @keyup.enter="onSubmit"
        >
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="User"
              clearable
            />
          </el-form-item>
  
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              show-password
              clearable
            />
          </el-form-item>
  
          <!-- 登录按钮 -->
          <el-form-item>
            <el-button 
              type="primary" 
              @click="onSubmit"
              :loading="loading"
              style="width: 100%"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  
  const router = useRouter()
  
  // 表单数据
  const loginForm = reactive({
    username: '',
    password: ''
  })
  
  // 表单验证规则
  const loginRules = reactive({
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
    ]
  })
  
  // 提交状态
  const loading = ref(false)
  const loginFormRef = ref(null)
  
  // 提交处理
  const onSubmit = () => {
    loginFormRef.value.validate(valid => {
      if (!valid) return
  
      loading.value = true
      
      // 这里模拟登录请求（实际项目中换成你的API）
      setTimeout(() => {
        loading.value = false
        
        // 模拟登录成功
        ElMessage.success('登录成功')
        router.push('/dashboard') // 跳转到后台主页
      }, 1000)
    })
  }
  </script>
  
  <style scoped>
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: linear-gradient(145deg, #f0f2f5 0%, #c1c3c7 100%);
  }
  
  .login-box {
    width: 400px;
    padding: 30px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
  
  .login-title {
    text-align: center;
    margin-bottom: 30px;
    color: #303133;
  }
  </style>
  